/** layuiAdmin.std-v1.2.1 LPPL License By http://www.layui.com/admin/ */
layui.define(["table", "form", "pkc", "element", "laypage"], function (e) {
    var t = layui.$,
        i = layui.table,
        r = (layui.form, layui.element),
        layform = layui.form,
        layer = layui.layer,
        pkc = layui.pkc;

    var form = {
        local: {
            fm: null //当前页面form元素
        },
        // 获取角色列表
        getRoleList: function(){
            return pkc.http.SendPost({},
              "cs.c02.get",
              function (ret) {
                  if (ret) {
                      ret = JSON.parse(ret);
                      if (ret !== null && ret.Status === "1") {
                          //赋值表单
                          t.each(ret.Data, function (i, v) {
                              t("#selroleid").append(
                                  "<option value='" +
                                  v.id +
                                  "' " +
                                  (i === 0 ? "selected" : "") +
                                  ">" +
                                  v.rolename +
                                  "</option>"
                              );
                              layform.render("select"); //刷新select选择框渲染
                          });
                      } else {
                          layer.alert(ret.Message);
                      }
                  }
              },
              function (res) {
                  layer.alert("网络异常.请稍后再试！");
                  // layer.closeAll("loading");
              }
          );
        },
        /**
         * 加载数据
         */
        loadData: function () {
            var data = parent.data;
            layform.val("fmapp", {
                username: data.username,
                userdisplayname: data.userdisplayname,
                enabled: data.enabled,
                roleid: data.roleid,
                remark: data.remark,
            });
        },
        /**
         * 初始化控件事件
         * */
        initEv: function () {
            //监听提交
            layform.on("submit(fmsub)", function (fm) {
                layform.verify();
                layer.confirm("需要保存数据?", {
                    icon: 3,
                    title: "提示"
                }, function (
                    index
                ) {
                    var ldIndex = layer.load(1);
                    var data = form.getWebParam(fm);
                    form.toolAction.save(data).always(() => {
                        layer.closeAll("loading");
                    });

                    layer.close(index);
                });

                // layer.msg(JSON.stringify(data.field));
                return false;
            });
        },
        /**
         * 界面初始化
         * */
        init: function () {
            this.initEv();
            this.getRoleList();
            form.loadData();
            setTimeout(function () {
                layer.closeAll("loading");
            }, 2000);
        },
        // 编辑
        toolAction: {
            /**
             * 新增
             * */
            save: function (data) {
              return pkc.http.SendPost(
                data,
                "cs.c01.update",
                function (ret) {
                  if (ret) {
                    ret = JSON.parse(ret);
                    if (ret !== null && ret.Status === "1") {
                      //success
                      layer.msg('操作成功');
                      form.close();
                      //重新加载数据
                      parent.reloadGrid(1, 10);
                    } else {
                      layer.alert(ret.Message);
                    }
                  }
                },
                function (res) {
                  layer.alert("网络异常.请稍后再试！");
                  // layer.closeAll("loading");
                }
              );
            },
            
          },
        getWebParam: function (fmData) {
        fmData = fmData.field;
        var data = {};
        data.id = parent.data.id;
        data.username = fmData.username;
        data.userdisplayname = fmData.userdisplayname;
        data.enabled  = t(
            'input[type="checkbox"][name="enabled"]'
        )[0].checked ?
        1 :
        0;
        data.roleid  = parseInt(fmData.roleid);
        data.remark = fmData.remark;
        return data;
        },
        /**
         * 关闭窗体
         */
        close: function () {
            var index = parent.layer.getFrameIndex(window.name);
            setTimeout(function(){
                parent.layer.close(index);
            },1000)
        }
    };

    e("usersetedit", {
        init: () => {
            form.init();
        }
    });
});